define([
    "dojo/_base/declare",
    "gistech/widgets/baseWidget/BaseWidget","dojo/on","esri/domUtils","dojo/mouse"],
    function (declare, BaseWidget,on,DomUtil,Mouse) {
        return declare([BaseWidget], {
            map:null,
            point:null,
            dragAble:false,
            constructor: function (args) {
            },
            postCreate: function () {
                this.inherited(arguments);
                DomUtil.hide(this.domNode);
                on(this.domNode,"mousedown",dojo.hitch(this,this.onMouseDown));
            },
            startup: function () {
                this.inherited(arguments);
            },
            show: function(point) {
            	if(!point)point=this.point;
            	this._registerMapListeners();
            	this.point=point;
            	DomUtil.show(this.domNode);
            	this._adjustPosition();
            	return this;
            },
            hide: function(a, b) {
            	this._unregisterMapListeners();
            	DomUtil.hide(this.domNode);
            },
            _adjustPosition: function() {
            	var sp=this.map.toScreen(this.point);
//            	console.log(sp)
            	ioc.map.testPoint = this.point;//TODO
                this.move(sp.x,sp.y);
            },
            _panListener:null,
            _panStartListener:null,
            _zoomListener:null,
            _extentListener:null,
            _registerMapListeners: function() {
                this._unregisterMapListeners();
                var tempThis=this;
                this._panListener=on(this.map,"Pan",dojo.hitch(this,this._pan));
                this._panStartListener=on(this.map,"PanStart",dojo.hitch(this,this._panStart));
                this._zoomListener=on(this.map,"ZoomStart",dojo.hitch(this,this.zoomStart));
                this._extentListener=on(this.map,"ExtentChange",dojo.hitch(this,this.extentChange));
            },
            _unregisterMapListeners: function() {
            	if(this._panStartListener)this._panStartListener.remove();
                if(this._panListener)this._panListener.remove();
                if(this._zoomListener)this._zoomListener.remove();
                if(this._extentListener)this._extentListener.remove();
            },
            _panZero:null,_panStart:function(e){
            	this._panZero=this.map.toScreen(this.point);
            },
            _pan:function(e){
            	if(!this._panZero) this._panZero=this.map.toScreen(this.point);
            	this.move(this._panZero.x+e.delta.x,this._panZero.y+e.delta.y);
            },
            zoomStart:function(){
            	DomUtil.hide(this.domNode);
            },
            extentChange:function(){
            	DomUtil.show(this.domNode);
            	this._adjustPosition();
            	this._panZero=null;
            },
            move:function(x,y){
            	
            	switch(ioc.map.ClusterMapLevel){
	            	case 4:
	            		x = x-120; 
	            		y = y-60;
	            	  break;
	            	case 5:
	            		x = x-103 
	            		y = y-50
	            	  break;
	            	case 6:
	            		x = x-108 
	            		y = y-48
	              	  break;
	            	case 7:
	            		x = x-100 
	            		y = y-40
	              	  break;
	            	case 8:
	            		x = x-100 
	            		y = y-30
	              	  break;
	            	case 9:
	            		x = x-130 
	            		y = y-36
	              	  break;
	            	default:
            	}
            	dojo.style(this.domNode, "left" ,x+"px");
                dojo.style(this.domNode, "top", y + "px");
            },
            destroy:function(){
            	this.inherited(arguments);
            	this._unregisterMapListeners();
            },
            /*鼠标按下时可能开始拖动，可覆盖以改变开始拖动的逻辑*/
            onMouseDown:function(e){
            	if(this.dragAble){
            		var off=this.getOffset(e.target,this.domNode);
            		this.startDrag([e.offsetX||e.layerX+off[0],e.offsetY||e.layerY+off[1]]);
            		var tempThis=this;
            		var onMouseUp=function(){
            			upEvent.remove();
            			tempThis.endDrag();
            		};
            		var upEvent=on(document.body,"mouseup",onMouseUp);
            	}
            },
            /*开始拖动（鼠标跟随）*/
            startDrag:function(offset){
            	var mask=dojo.toDom('<div style="position:relative;width:100%;height:100%;z-index:9999;background-color: rgb(255, 0, 0);background-alpha:0;opacity:0.1;-moz-opacity:0.1;filter:alpha(opacity=10)"></div>');
            	this.map.disablePan();
            	this._unregisterMapListeners();
            	esri.config.defaults.map.panDuration=0;
            	this.map.root.appendChild(mask);
            	this.map.offset=offset||[0,0];
            	this.map.dragMask=mask;
            	this.map.mousemoveEvent=on(this.map.root,"mousemove",dojo.hitch(this,this.dragMove));
            	this.map.oldPosition=this.map.toScreen(this.point);
            	this.map.oldOffset=[this.domNode.offsetLeft,this.domNode.offsetTop];
            },
            /*结束拖动*/
            endDrag:function(){
            	clearInterval(this.map.inner);
            	this.map.root.removeChild(this.map.dragMask);
            	if(this.map.mousemoveEvent)this.map.mousemoveEvent.remove();
            	this.map.oldPosition.x+=this.domNode.offsetLeft-this.map.oldOffset[0];
            	this.map.oldPosition.y+=this.domNode.offsetTop-this.map.oldOffset[1];
            	this.point=this.map.toMap(this.map.oldPosition);
            	this._adjustPosition();
            	this.map.enablePan();
            	this._registerMapListeners();
            	esri.config.defaults.map.panDuration=350;
            },
            /*拖动过程中触发*/
            dragMove:function(e){
            	dojo.style(this.domNode,"left",e.offsetX||e.layerX-this.map.offset[0]+"px");
            	dojo.style(this.domNode,"top",e.offsetY||e.layerY-this.map.offset[1]+"px");
            	this.testEdge(e);
            },
            /*获取dom相对于oDom的坐标，两者之间必须存在直系布局关系*/
            getOffset:function(dom,oDom){
            	if(dom==oDom){
            		return [0,0];
            	}
            	if(dom.offsetParent){
            		var poff=this.getOffset(dom.offsetParent,oDom);
            		return [dom.offsetLeft+poff[0],dom.offsetTop+poff[1]];
            	}else{
            		return [0,0];
            	}
            },
            /*测试是否拖动到地图边缘，如果是则移动地图*/
            testEdge:function(e){
            	clearInterval(this.map.inner);
            	var box=dojo.marginBox(this.map.root);
            	var le=e.offsetX||e.layerX;
            	var re=box.w-(e.offsetX||e.layerX);
            	var te=e.offsetY||e.layerY;
            	var be=box.h-(e.offsetY||e.layerY);
            	var delta=30;
            	console.log(le,re,te,be,box);
            	if(le<delta){
            		this.map.inner=setInterval(dojo.hitch(this,this.onEdge,"l"),30);
            	}else if(re<delta){
            		this.map.inner=setInterval(dojo.hitch(this,this.onEdge,"r"),30);
            	}else if(te<delta){
            		this.map.inner=setInterval(dojo.hitch(this,this.onEdge,"t"),30);
            	}else if(be<delta){
            		this.map.inner=setInterval(dojo.hitch(this,this.onEdge,"b"),30);
            	}else{
            		clearInterval(this.map.inner);
            	}
            },
            onEdge:function(dir){
            	dx=this.map.extent.getWidth()/100;
            	dy=this.map.extent.getHeight()/100;
            	var point=this.map.extent.getCenter();
            	switch(dir){
            		case "l":
            			point.x-=dx;
            			break;
            		case "r":
            			point.x+=dx;
            			break;
            		case "t":
            			point.y+=dy;
            			break;
            		case "b":
            			point.y-=dy;
            			break;
            	}
            	this.map.centerAt(point);
            }
        });
    });